﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MTO.Library.Domain;
using MTO.Library.Domain.Repositories;
using System.Data.Entity;
using MTO.Library.Data.EF;

namespace MTO.Library.Data
{
    public class CustomerRepository : ICustomerRepository
    {
        Entities appContext;

        public CustomerRepository(Entities appContext)
        {
            this.appContext = appContext;
        }
        
        public IQueryable<Customer> GetQuery()
        {
            return appContext.Customers;
        }

        public IQueryable<Customer> Search(string searchKeyword)
        {
            var q = from customer in this.GetQuery()
                    select customer;

            return q;
        }

        public Customer GetById(int id)
        {
            return appContext.Customers.Single(x => x.CustomerId == id);
        }

        public virtual void SaveOrUpdate(Customer customer)
        {
            if (customer.CustomerId == 0)
            {
                appContext.Customers.Add(customer);
            }

            appContext.SaveChanges();
        }
    }
}
